/// var FMShipping = { init: function () { if (typeof $('.pagina-carrinho').html() === 'undefined') { return; } console.log('Shipping :D'); FM.setStorage('_fmais__close', false); // valida se as classes padrões já iniciaram var idValid = setInterval(function () { if (typeof FMC === 'undefined' || typeof FM === 'undefined' || typeof FMShipping.settings === 'undefined') { return; } clearInterval(idValid); }); // valida se é local ou prod if (!FMShipping.settings.isPage()) { if (!FM.isLocal) { return; } } var count = 0; // valida se as classes já iniciaram e se o app esta ativo var idS = setInterval(function () { // valida se o app esta ativo if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { count++; if (count === 50) { clearInterval(idS); } //console.log('Shipping offline :('); return; } // encerra a rotina clearInterval(idS); //// se for valido retorna //if (FM.isStorage(FM.keys.shipping)) { // FMC.file(FM.getStorage(FM.keys.shipping)); // return; //} if ($('input[name=cep_destino]').val() !== undefined) { FMShipping.settings.zipCode = $('input[name=cep_destino]').val(); } if ($('input[name=cep]').val() !== undefined) { FMShipping.settings.zipCode = $('input[name=cep]').val(); } // lista as configurações salvas na base // FMShipping.findZip(FMShipping.settings.zipCode, FMShipping.settings.price); }, FM.timeInterval); // seta a base de informações do frete grátis var idSs = setInterval(function () { if (!FM.isStorage(FM.keys.shipping)) { return; } FMShipping.settings.data = FM.getStorage(FM.keys.shipping); if (FMShipping.settings.data !== 'undefined' && FMShipping.settings.data !== null) { // encerra a rotina clearInterval(idSs); } }, FM.timeInterval); // lista as informações do cep atual if (FMC.isActive(FM.apps, FMShipping.settings.appId)) { FMShipping.findZip(FMShipping.settings.zipCode, FMShipping.price()); } // pesquisa status do cep por valor e cep $(document).on('keyup', 'input[name=cep_destino]', function (e) { if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { return } if (FMShipping.settings.isPage()) { var zip = $(this).val(); if ((typeof zip !== 'undefined') && (zip === "__.___-___") || (zip.length < 8)) { return; } FM.setStorage('_fmais__close', false); FMShipping.settings.zipCode = zip; FMShipping.findZip(zip, FMShipping.price()); } }); $(document).on('keyup', 'input[name=cep]', function (e) { if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { return } if (FMShipping.settings.isPage()) { var zip = $(this).val(); if ((typeof zip !== 'undefined') && (zip === "__.___-___") || (zip.length < 8)) { return; } FM.setStorage('_fmais__close', false); FMShipping.settings.zipCode = zip; FMShipping.findZip(zip, FMShipping.price()); } }); //setInterval(function () { // if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { // return; // } // var data = FMShipping.getData(); // if (data === null) { // return; // } // var zip = ''; // if ($('input[name=cep_destino]').val() !== undefined) { // zip = $('input[name=cep_destino]').val(); // if ((typeof zip !== 'undefined') && (zip !== "__.___-___") && (zip.length >= 8)) { // FMShipping.settings.zipCode = zip; // } // } // if ($('input[name=cep]').val() !== undefined) { // zip = $('input[name=cep]').val(); // if ((typeof zip !== 'undefined') && (zip !== "__.___-___") && (zip.length >= 8)) { // FMShipping.settings.zipCode = zip; // } // } // zip = ((typeof zip !== 'undefined') ? zip : (document.getElementsByName('cep_destino')[0] === undefined) ? ((document.getElementsByName('cep')[0] !== undefined) ? document.getElementsByName('cep')[0].value : '') : document.getElementsByName('cep_destino')[0].value).replace('-', ''); // FMShipping.findZip(zip, FMShipping.price()); //}, FM.timeInterval); //valida se o app esta valido para apresentar a mensagem setInterval(function () { if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { return; } FMShipping.findZip(FMShipping.settings.zipCode, FMShipping.price()); }, FM.timeToLoad); }, getData: function () { var data = document.getElementById('apps-json-shipping-data-new'); if (data === null) { data = document.getElementById('apps-json-shipping-data'); } if (data === null) { return; } FM.setStorage(FM.keys.shipping, JSON.parse(data.innerHTML), FM.time); return JSON.parse(data.innerHTML); }, alert: function (title, message, type) { //if (document.getElementById('fmais-shippings-alert-body') !== null) { // if (document.getElementById('fmais-shippings-alert-body').style.display === 'none') { // document.getElementById('fmais-shippings-alert-body').style.display = 'block'; // } //} if (document.getElementById('fmais-shippings-alert-body') !== null) { if (document.getElementById('fmais-shippings-alert-body').style.display === 'none' && !FM.getStorage('_fmais__close')) { document.getElementById('fmais-shippings-alert-body').style.display = 'block'; } } FMShipping.settings.createAlert(title, message, type); FM.removeAddClass('#fmais-shippings-alert-body', 'fmais-shipping-danger,fmais-shipping-warning,fmais-shipping-info,fmais-shipping-success', 'fmais-shipping-' + type + ''); }, alertNotZip: function (title, content, type) { var title = (typeof title === 'undefined') ? 'Quer ganhar frete grátis?' : title; var content = (typeof content === 'undefined') ? 'Informe o seu cep e saiba quanto falta para ganhar.' : content; var type = (typeof type === 'undefined') ? 'danger' : type; if (typeof FMShipping.settings.zipCode !== 'undefined' && FMShipping.settings.zipCode !== '' && FMShipping.settings.zipCode.length >= 8) { content = 'Por favor aguarde, estamos verificando se você ganhou frete grátis'; type = 'info'; } FMShipping.settings.createAlert(title, content); FM.removeAddClass('#fmais-shippings-alert-body', 'fmais-shipping-danger,fmais-shipping-warning,fmais-shipping-info,fmais-shipping-success', 'fmais-shipping-' + type + ''); }, findZip: function (zip, price) { if (!FMC.isActive(FM.apps, FMShipping.settings.appId)) { return; } var data = FMShipping.getData(); if (data === null || typeof data === 'undefined') { return; } zip = ((typeof zip !== 'undefined') ? zip : (document.getElementsByName('cep_destino')[0] === undefined) ? ((document.getElementsByName('cep')[0] !== undefined) ? document.getElementsByName('cep')[0].value : '') : document.getElementsByName('cep_destino')[0].value).replace('-', ''); if (zip.length < 8) { FMShipping.alertNotZip(); return; } var message = 'Ops! Não há opção de frete grátis para sua região.'; var title = 'FRETE GRÁTIS'; var type = 'info'; var exits = false; try { var newData = []; if (data.length > 0) { data.find(function (element) { if (element.faixaInicial <= parseInt(zip) && parseInt(zip) <= element.faixaFinal) { newData.push(element) } }); } var dataOk = newData.sort(); data = []; data.push(dataOk[0]); } catch (e) { } var ok = data.find(function (element) { if (typeof element === 'undefined') { FMShipping.none(); return; } if (element.faixaInicial <= parseInt(zip) && parseInt(zip) <= element.faixaFinal) { if (element.regiao.length === 0) { var zipOne = 0; var zipTwo = 0; try { FM.getAd(zip).onreadystatechange = (function () { if (FM.completed(this)) { var json = JSON.parse(this.responseText); if (typeof json.message !== 'undefined') { return; } var el = data.find(function (elements) { return elements.regiao.find(function (regiao) { return regiao.toLowerCase() === json.data.state.toLowerCase() }); }); zipOne = element.faixaInicial; zipTwo = element.faixaFinal; if (element.faixaInicial < zipOne || element.faixaFinal > zipTwo) { console.log(zipOne); console.log(zipTwo); return; } var newPrices = price.toString().split('.'); var newPrice = 0; if (newPrices.length > 1) { var priceDecimal = newPrices[1] if (priceDecimal.length > 2) { newPrice = (newPrices[0] + newPrices[1]); } else { newPrice = newPrices[0] + '.' + newPrices[1]; } } else { newPrice = newPrices[0] + '.' + newPrices[1]; } var valorMinimos = element.valorMinimo.toString().split('.'); var valorMinimo = 0; if (valorMinimos.length > 1) { var priceDecimal = valorMinimos[1] if (priceDecimal.length > 2) { valorMinimo = (valorMinimos[0] + valorMinimos[1]); } else { valorMinimo = valorMinimos[0] + '.' + valorMinimos[1]; } } else { valorMinimo = valorMinimos[0] + '.' + valorMinimos[1]; } if (parseFloat(valorMinimo) <= parseFloat(newPrice)) { exits = true; message = element.mensagemGanhou; title = element.titulo; type = 'success'; } else if (parseFloat(valorMinimo) > parseFloat(newPrice)) { exits = true; var falta = (parseFloat(valorMinimo) - parseFloat(price)).toFixed(2); message = element.mensagemFaltaTanto.replace('{0}', falta.replace('.', ',')); title = element.titulo; type = 'warning'; } //console.log(message); //console.log(title); //console.log(type); if (el === null || el === undefined) { exits = false; } if (!exits) { FMShipping.none(); return; } FMShipping.alert(title, message, type); } else if (FM.error(this)) { //FM.setStorage(FM.keys.present, data, FM.time); } }); } catch (e) { console.log(e); } } else { var zipOne = element.faixaInicial; var zipTwo = element.faixaFinal; if (element.faixaInicial < zipOne || element.faixaFinal > zipTwo) { console.log(zipOne); console.log(zipTwo); return; } var newPrices = price.toString().split('.'); var newPrice = 0; if (newPrices.length > 1) { var priceDecimal = newPrices[1] if (priceDecimal.length > 2) { newPrice = (newPrices[0] + newPrices[1]); } else { newPrice = newPrices[0] + '.' + newPrices[1]; } } else { newPrice = newPrices[0] + '.' + newPrices[1]; } var valorMinimos = element.valorMinimo.toString().split('.'); var valorMinimo = 0; if (valorMinimos.length > 1) { var priceDecimal = valorMinimos[1] if (priceDecimal.length > 2) { valorMinimo = (valorMinimos[0] + valorMinimos[1]); } else { valorMinimo = valorMinimos[0] + '.' + valorMinimos[1]; } } else { valorMinimo = valorMinimos[0] + '.' + valorMinimos[1]; } if (parseFloat(valorMinimo) <= parseFloat(newPrice)) { exits = true; message = element.mensagemGanhou; title = element.titulo; type = 'success'; FMShipping.alert(title, message, type); } else if (parseFloat(valorMinimo) > parseFloat(newPrice)) { exits = true; var falta = (parseFloat(element.valorMinimo) - parseFloat(price)).toFixed(2); message = element.mensagemFaltaTanto.replace('{0}', falta.replace('.', ',')); title = element.titulo; type = 'warning'; FMShipping.alert(title, message, type); } //console.log(message); //console.log(title); //console.log(type); } } }); var zips = []; var start = []; var end = []; var newZip = parseInt(zip); if (typeof data === 'undefined') { return; } $.each(data, function (index, item) { if (typeof item === 'undefined') { return; } if (item.faixaInicial <= newZip && newZip <= item.faixaFinal) { start.push(item.faixaInicial); end.push(item.faixaFinal); zips.push(item); } }); start = start.sort(); end = end.sort(); var maxZip = start[0]; var minZip = end[0] if (!exits) { FMShipping.none(); return; } window.onload = function () { try { FM.getAd(zip).onreadystatechange = (function () { if (FM.completed(this)) { var json = JSON.parse(this.responseText); if (typeof json.message !== 'undefined') { return; } var el = data.find(function (elements) { return elements.regiao.find(function (regiao) { return regiao.toLowerCase() === json.data.state.toLowerCase() }); }); if (el === null || el === undefined) { FMShipping.none(); return; } else { FMShipping.alert(title, message, type); } } else if (FM.error(this)) { //FM.setStorage(FM.keys.present, data, FM.time); } }); } catch (e) { console.log(e); } }; FMShipping.alert(title, message, type); }, none: function () { if (document.getElementById('fmais-shippings-alert-body') !== null) { if (document.getElementById('fmais-shippings-alert-body').style.display !== 'none') { document.getElementById('fmais-shippings-alert-body').style.display = 'none'; } } }, getValueShipping: function () { var shippings = document.getElementsByName('formaEnvio'); var result = 0; if (shippings !== null && shippings.length > 0) { $.each(shippings, function (index, item) { if (item.checked) { result = parseFloat(item.getAttribute('data-valor')); } }); } return result; }, price: function () { if (typeof $('.carrinho-checkout').html() === 'undefined') { return (FMShipping.settings.priceTotal() - FMShipping.getValueShipping()); } else { var shipping = $('.frete-preco strong').html(); if (typeof shipping === 'undefined') { shipping = '0'; } try { shipping = $('.frete-preco strong').html().toString().replace(/\s/g, '').replace('R$', '').replace('.', '').replace(',', '.'); shipping = parseFloat(shipping); } catch (e) { shipping = 0; } var total = $('.total').data('total'); return (total - shipping); } }, settings: { appId: 11, zipCode: ($('input[name=cep_destino]').val() !== undefined) ? $('input[name=cep_destino]').val() : ($('input[name=cep]').val() ? $('input[name=cep]').val() : ''), priceTotal: function () { var price = (document.querySelector('div.total .valor-total') !== null ? document.querySelector('div.total .valor-total').getAttribute('data-total-valor') : "0"); var first = price.split('.').length > 0 ? price.split('.')[0] : '0'; var last = price.split('.').length > 1 ? price.split('.')[1] : '0'; return parseFloat(first + '.' + last); }, priceSubtotal: ($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === null || typeof $("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === 'undefined') ? ((typeof $('table.tabela-carrinho tr .padding-preco .subtotal').data('float') === 'undefined') ? 0 : parseFloat($('table.tabela-carrinho tr .padding-preco .subtotal').data('float'))) : parseFloat($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor')), price: ($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === null || typeof $("div.subtotal strong.titulo.cor-principal").data('subtotal-valor') === 'undefined') ? ((typeof $('table.tabela-carrinho tr .padding-preco .subtotal').data('float') === 'undefined') ? 0 : parseFloat($('table.tabela-carrinho tr .padding-preco .subtotal').data('float'))) : parseFloat($("div.subtotal strong.titulo.cor-principal").data('subtotal-valor')), discount: parseFloat(((document.getElementById('cupom_desconto') !== null) ? document.getElementById('cupom_desconto').getAttribute('data-desconto-valor') : "0")), discountTotal: ((document.getElementById('cupom_desconto') !== null && document.getElementById('cupom_desconto').getAttribute('data-desconto-aplicar-no-total') === 'não') ? false : true), data: null, isPage: function () { var pages = 'carrinho,carrinho/index,/carrinho/index,checkout,checkout/,/checkout/'.split(','); var ok = false; var page = window.location.pathname.replace('/index', ''); var firstPage = page.replace('index', ''); var newPage = firstPage.replace('/', ''); var lastPage = newPage.replace('/', ''); if (lastPage === '') { return ok; } for (var i = 0; i < pages.length; i++) { if (pages[i].indexOf(lastPage) >= 0) { ok = true; } } return ok; }, createAlert: function (title, content, type) { var div = document.createElement("div"); var html = '
'; html += '
'; html += '
' + title + '
'; html += '
X
'; html += '
'; html += '
'; html += '
' + content + '
'; html += '
'; html += '
'; div.innerHTML = html; var exists = document.getElementById('fmais-shippings-alert-body'); if (exists === undefined || exists === null) { document.body.appendChild(div); } else { document.getElementById('fmais-shippings-alert-header-title').innerHTML = title; document.getElementById('fmais-shippings-alert-content-p').innerHTML = content; } var x = document.getElementById("fmais-shippings-alert-header-close"); if (x.addEventListener) { x.addEventListener("click", function () { FM.setStorage('_fmais__close', true); document.getElementById('fmais-shippings-alert-body').style.display = 'none'; }); } else if (x.attachEvent) { x.attachEvent("onclick", function () { document.getElementById('fmais-shippings-alert-body').style.display = 'none'; }); } if (typeof FMShipping.settings.zipCode === 'undefined') { document.getElementById('fmais-shippings-alert-body').style.display = 'none'; } }, } }; (function () { 'use strict' FMShipping.init(); })();